//
//  TableVc.swift
//  one
//
//  Created by huangfeng on 2018/4/28.
//  Copyright © 2018年 huangfeng. All rights reserved.
//

import UIKit

class TableVc: UIViewController{

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
        let v = UIView(frame: CGRect(x: 64, y: 64, width: 100, height: 100))
        v.backgroundColor = UIColor.red
        view.addSubview(v)
        // Do any additional setup after loading the view, typically from a nib.
        
        let btn = UIButton(type: .contactAdd)
        v.addSubview(btn)
        btn.addTarget(self, action: #selector(clickMe), for: .touchUpInside)
        
        v.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)  //FIXME: 需要修改颜色
        
        let iv = UIImageView(image: #imageLiteral(resourceName: "icon_message_main")) // TODO: 应用更改头像
        iv.center = view.center
        view.addSubview(iv)
        
        // 自动推导  热键： option + click
        demo()
        
        demo1()
        /*
         // 在开发中  关于var 和 let
         通常先使用let 在需要变化的时候 再改为 var
         */
        
        let x: Optional = 10
        let y: Int? = 20
        //强行解包  如果 变量的值是 nil 就会崩溃
        // ？ 是定义用的  ！解包用的
        print(x! + y!)
        
        //let y:Int? 可选项 没有默认值
        // var  y:Int? 可选项 默认值为 nil
        
        demo5(x: 10, y: 20)
        demo18()
        
        
        let onebtn = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 30))
        onebtn.addTarget(self, action: #selector(clickMe(btn:)), for: .touchUpInside)
        onebtn.setTitle("返回", for: .normal)
        onebtn.setTitleColor(#colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1), for: .normal)
        onebtn.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        onebtn.layer.cornerRadius = 5
        onebtn.layer.shadowOffset = CGSize(width: 100, height: 20)
        onebtn.layer.shadowColor = UIColor.black.cgColor
        view.addSubview(onebtn)
        
        
        
        let p = Student()
        print("\(p.name) ~~~~~ \(p.no)")
        
        let s = Student(no: "2")
        print(s.no)
        
        let dictc = ["name" : "3"]
        
        let news = Wuman(dict: ["name" : "3"])
        print(news.name)
        
        print(Wuman.propertyList())
        
        Animation.propertyList()
        // Do any additional setup after loading the view.
    }
    
    
    // MARK: 字典  增删改
    func demo18() {
        
        // swift 用[] 初始化
        var dict = ["name":"张","age":10] as [String : Any]
        dict["name"] = "大哥"
        
        dict["title"] = "好"
        
        
        for e in dict {
            print("\(e.key) \(e.value)")
        }
        
        
        //字典不能直接相加
        var dict1 = ["name":"张超","age":10] as [String : Any]
        for e in dict1 {
            dict[e.key] = dict1[e.key]
        }
        print(dict)
        
    }
    
    // MARK: 字典  增删改
    func demo17() {
        
        // swift 用[] 初始化
        var dict = ["name":"张","age":10] as [String : Any]
        dict["name"] = "大哥"
        
        dict["title"] = "好"
        
        
        
        
        // key 来定位 key必须是可以 hash 的 MD5一种   hash 将
        // hash 就是将字符串变成唯一的 “整数” 便于查找，提高字典遍历的速度
        dict.removeValue(forKey: "age")
        dict.removeAll()
        
    }
    
    
    // MARK: 字典
    func demo16() {
        
        // swift 用[] 初始化
        let dict = ["name":"张","age":10] as [String : Any]
        let array:[[String:Any]] = [
            ["name":"李","age":10],
            ["name":"张","age":20]
        ]
        
        
        
        for item in array {
            print(item["name"] ?? "",item["age"] ?? "")
        }
        
    }
    
    
    //数组增删改
    func demo15() {
        var array = [1,2,3,4,5]
        array.append(6)
        
        array[0] = 7
        
        print(array)
        array.remove(at: 2)
        array.removeAll(keepingCapacity: true) //删除所有  保持容量
        
        var arr = [Int]()
        for i in 0..<8 {
            arr.append(i)
            print("\(arr) 容量 \(arr.capacity)")
        }
        
        let arr4 = [1,2,3,4,5]
        // 两个数组合并 数组类型需要一致
        let arr1 = [5,6,9,10]
        let arr3 = arr4 + arr1
        print(arr3)
    }
    
    
    //数组有 6种用法
    func demo14() {
        let array = ["lilis","消防","xiaoyang"]
        let array1 = [456,123];
        print(array)
        
        let p  = CGPoint(x: 10, y: 300)
        let array3 = [p]
        let array4 = ["12",12,NSValue(cgPoint: p)] as [Any]
        // 在混合数组中 cg POINt 需要包装
        
        
        var someInts = [Int]()
        print("\(someInts.count)++++++++")
        someInts.append(3)
        print("\(someInts.count)++++++++")
        someInts = []
        print("\(someInts.count) \(array4) \(array3) \(array1)++++++++")
        
        let arr5 = NSMutableArray(array: [1,2,3,4,5])
        for x in arr5 {
            print(x)
            // 1,2,3,4,5
        }
        
        //dropfirst()  弃除 前2个 元素
        
        for x in arr5.dropFirst(2) {
            print(x)
            //            arr5.dropLast()    不循环 最后的几个元素
        }
        
        // 带数组下标的遍历  先返转  再转元组
        for (num,element) in arr5.reversed().enumerated() {
            print(num,element)
        }
        
        for e in arr5.enumerated() {
            print(e.element,e.offset)  //offset 数组下标 element 是值
        }
        
        for i in 0..<arr5.count {
            print(arr5[i])
        }
        
        
    }
    
    
    // MARK: 字符串子串
    func demo13() {
        //一般使用 nsstring 作为中转
        
        let btn = UIButton(frame: CGRect(x: 50, y: 80, width: 60, height: 20))
        let str = "我们一起去"
        let ocStr = str as NSString
        let s1 = ocStr.substring(to: 3)
        let s2 = ocStr.substring(with: NSMakeRange(2, 3))
        
        //stirng 的 3.0
        print(s1,s2)
        
        
    }
    
    // MARK: 字符串拼接和格式化
    func demo12() {
        let str = "hello world"
        print(str.lengthOfBytes(using: .utf8))
        str.count  // OC length
        
        let ocstr = str as NSString
        ocstr.length
        
        //p字符串拼接
        
        let name = 18
        let age: String? = "老李"
        let point = CGPoint(x: 100, y: 200)
        let strr = "\(name) \(age ?? "") \(point)"  //如果直接写 age 输入会带有 optional
        print(strr)
        
        let datestr1 = String(format: "%02d:%02d:%02d", name,name,name)
        print(datestr1)
    }
    
    //MARK: 字符串遍历
    func demo11() {
        let str = "我要飞的更高"
        for c in str {
            print(c)
        }
    }
    
    
    
    func demo10() {
        //反序遍历
        for i in (0..<10).reversed() {
            print(i)
        }
    }
    
    func demo9() {
        //switch 可以用swfitch 匹配
        let point = CGPoint(x: 10, y: 10)
        
        switch point {
        case let p where p.x == 0 && p.y == 0 :
            print("原点")
        case let p where p.x == 0:
            print("Y轴")
        case let p where abs(p.x) == abs(p.y):
            print("对角线")
        default:
            print("其它")
        }
    }
    
    
    
    
    
    
    
    //MARK: guard 守卫 / 守护
    // grard let 和 if let 相反
    func demo8() {
        let oName: String? = "123"
        let oAage: Int? = 10
        
        
        
        guard let name = oName,let age = oAage else {
            print("name 或着 age 为 Nil")
            return
        }
        
        print(name + String(age))
        
        
    }
    
    
    
    // MARK: if let /var 连用语法 目的就是判断值   不是单纯的if判断
    func demo7() {
        let oName: String? = "123"
        let oAage: Int? = 10
        
        if var name = oName,
            let age = oAage {
            //进行分支以后  name 和 age  一定是有值的  没有值会进入else判断
            print(name + String(age))
        }else{
            print("name 或着 age 为 Nil")
        }
    }
    
    
    /** MARK: ??   ??是一个简单的三目  如果有值 ，使用值 如果没有值 使用？？后面的值替代
     ?? 优先级低  最好用（）包含一下
     */
    func demo6(x: Int?,y: Int?) {
        
        print((x ?? 0) + (y ?? 0))
        
        
    }
    
    func demo5(x: Int?,y: Int?) {
        if x != nil && y != nil {
            print(x! + y!)
        }else{
            print("x或着y 为空")
        }
        
    }
    
    
    // MARK: 三目
    func demo4() {
        let x = 10
        x > 5 ? print("大了") : print("小了")
    }
    
    func demo3() {
        let x = 10
        if x > 5 {
            
        }
    }
    
    
    func demo1 ()   {
        let x = 10
        let y = 10.5
        //不会做默认的限式转换，所有的类型确定，都有程序员自己负责
        
        /*
         Double(x)  这是一个结构体的 构造函数
         */
        let c = Double(x) + y
        
        print( c )
        
    }
    
    func demo()  {
        var x = 10
        x = 20
        
        _ = Double.pi
        
        
        //pow 求 平方数  2的3次方
        let d = pow(2, 3)
        print(d)
        
    }
    
    
    
    // 闭包中 需要使用self.
    
    /// 方法注释
    ///
    /// - Parameter btn:
    @objc func clickMe(btn: UIButton) -> () {
        //        print(#function)
        //        let tv = UITableView(frame: view.bounds, style: .plain)
        //        view.addSubview(tv)
        //        tv.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
        //        tv.dataSource = (self as! UITableViewDataSource)
        self .dismiss(animated: true, completion: nil)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
